package com.kaochong.xcx.web.controller;

import com.kaochong.xcx.common.constant.CommonConstant;
import com.kaochong.xcx.enums.ResultEnum;
import com.kaochong.xcx.exception.KaoChongException;
import com.kaochong.xcx.entity.WordBook;
import com.kaochong.xcx.pojo.cache.WordModel;
import com.kaochong.xcx.pojo.vo.WordBookVO;
import com.kaochong.xcx.service.WordBookService;
import com.kaochong.xcx.service.WordService;
import com.kaochong.xcx.utils.response.ResponseUtils;
import com.kaochong.xcx.utils.response.ResultVO;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * @author han
 * @date 2018-05-09 15:45
 */
@RestController
@RequestMapping("/recite/word")
@Slf4j
public class WordController {
	
	@Autowired
	WordService wordService;
	
	@Autowired
	WordBookService wordBookService;
	
	@GetMapping("/resetLevelStageWords/{level}/{number}/{auth}")
	public ResultVO resetLevelStageWords(@PathVariable Integer level, @PathVariable Integer number, @PathVariable String auth) {
		if (StringUtils.isBlank(auth)) {
			log.error("【背单词-更新阶段单词数量】auth is not null");
			throw new KaoChongException(ResultEnum.AUTHENTICATION_FAIL_ERROR);
		}
		if (!CommonConstant.REFRESH_WORD_AUTH.equals(auth)) {
			log.error("【背单词-更新阶段单词数量】auth error");
			throw new KaoChongException(ResultEnum.AUTHENTICATION_FAIL_ERROR);
		}
		WordBook wordBook = wordBookService.getWordBook(level);
		if (number == 0) {
			number = wordBook.getStageWordNum();
		}
		wordService.resetLevelStageWords(wordBook.getTarget(), wordBook.getMaxStudyStage(), number);
		return ResponseUtils.success();
	}
	
	@GetMapping("/resetEnSex/{level}/{auth}")
	public ResultVO resetEnSex(@PathVariable Integer level, @PathVariable String auth) {
		if (StringUtils.isBlank(auth)) {
			log.error("【背单词-更新词性】auth is not null");
			throw new KaoChongException(ResultEnum.AUTHENTICATION_FAIL_ERROR);
		}
		if (!CommonConstant.REFRESH_WORD_AUTH.equals(auth)) {
			log.error("【背单词-更新词性】auth error");
			throw new KaoChongException(ResultEnum.AUTHENTICATION_FAIL_ERROR);
		}
		WordBook wordBook = wordBookService.getWordBook(level);
		wordService.resetEnSex(wordBook.getTarget());
		return ResponseUtils.success();
	}
	
	@GetMapping("/resetByLever/{level}/{auth}")
	public ResultVO resetWordByLever(@PathVariable Integer level, @PathVariable String auth) {
		if (StringUtils.isBlank(auth)) {
			log.error("【背单词-更新阶段单词】auth is not null");
			throw new KaoChongException(ResultEnum.AUTHENTICATION_FAIL_ERROR);
		}
		if (!CommonConstant.REFRESH_WORD_AUTH.equals(auth)) {
			log.error("【背单词-更新阶段单词】auth error");
			throw new KaoChongException(ResultEnum.AUTHENTICATION_FAIL_ERROR);
		}
		WordBook wordBook = wordBookService.getWordBook(level);
		wordService.resetWordByLevel(wordBook.getTarget());
		return ResponseUtils.success();
	}
	
	@GetMapping("/book/list")
	public ResultVO wordbook(Map<String, Object> map) {
		List<WordBookVO> list = wordBookService.getAllOnShowList();
		map.put("bookList", list);
		return ResponseUtils.success(map);
	}
	
	@GetMapping("/newbook/list")
	public ResultVO book(Map<String, Object> map) {
		List<WordBookVO> list = wordBookService.getNewBookList();
		map.put("newbooks", list);
		return ResponseUtils.success(map);
	}
	
	/**
	 * 复习单词（待废除）
	 * @param map
	 * @param target
	 * @param stage
	 * @return
	 */
	@GetMapping("/stage")
	public ResultVO stageWords(Map<String, Object> map, Integer target, Integer stage){
		List<WordModel> wordList = wordService.stageWords(target, stage);
		map.put("wordList", wordList);
		return ResponseUtils.success(map);
	}
}
